<?php

/* 
 * db_help升级版
 * @2015-12-25 wzl
 * set_time_limit(0);
 * mysql_query('set global max_allowed_packet = 2*1024*1024*10');        
 */
/**db_help升级版*/
class db_help extends db_mysqli
{
    private $username = null;
    private $password = null;
    private $hostname = null;
    private $database = null;
    private $port = null;
    private $table_ext = null;
    private $charset = null;
    protected $error_info = '';
    /**表名*/
    protected $table_name = null;
    public function __construct() {        
        $file = common::get_config_file('dbconfig');
        include($file);
        $this->username = $db_user;
        $this->password = $db_pass;
        $this->hostname = $db_host;
        $this->database = $db_database;
        $this->table_ext = $prefix;
        $this->port = $db_port;
        $this->charset = $db_charset;
        parent::__construct($this->username, $this->password, $this->hostname, $this->database, $this->table_ext, $this->port, $this->charset);
    }
    /**
     * 返回行数
     * @param string $fields 字段，兼容null,*,fields
     * @param string $where 条件，兼容array, string
     * @return int
     */
    final public function getCount($fields , $where){
        $sql = $this->create_sql($fields, $where);
        return $this->result_rows($sql);
    }
    /**
     * 返回结果集 二维
     * @param string $fields 字段，兼容null,*,fields
     * @param string $where 条件，兼容array, string
     * @return array 二维结果
     */
    final public function getAll($fields = null , $where = null){   
        $sql = $this->create_sql($fields, $where);
        return $this->result_array($sql); 
    }
    /**
     * 获取一列数据
     * @param string $fields 字段，兼容null,*,fields
     * @param string $where 条件，兼容array, string
     * @return type
     */
    final public function getCol($fields , $where){
        $sql = $this->create_sql($fields, $where);
        return $this->result_col($sql);
    }
    /**
     * 返回结果集,一维
     * @param string $fields 字段，兼容null,*,fields
     * @param string $where 条件，兼容array, string
     * @return array 一维结果
     */
    final public function getRow($fields , $where){
        $sql = $this->create_sql($fields, $where);   
        return $this->result_row($sql);
    }  

    /**
     * 返回一个结果集
     * @param string $fields 字段，兼容null,*,fields
     * @param string $where 条件，兼容array, string
     * @return mix 一个结果
     */
    final public function getOne($fields, $where){
        $sql = $this->create_sql($fields, $where);
        return $this->result_one($sql);
    }
    
    /**
     * 新增
     * @param array $data 兼容array ,string
     * @return 返回成功与否，需要获取insert_id请调用insert_id()函数
     */
    final public function insert($data) {
        $sql = "insert {$this->table_name} set {$this->fields_create($data)}";
        mylog($sql, 'sql');
        return $this->query($sql);
    }
    /**
     * 更新
     * @param string $data 字段，兼容array , string
     * @param string $where 条件，兼容array, string
     * @return boolean 成功与否
     */
    final public function update($data, $where){
        $fieldsforupdate = $this->fields_create($data);
        $where_sql = $this->where_sql_and($where);
        $sql = "update {$this->table_name} set {$fieldsforupdate}  {$where_sql}";
        return $this->query($sql);
    }
    /**
     * 删除记录
     * @param type $where
     * @return type
     */
    final public function delete($where) {
        $where_sql = $this->where_sql_and($where);
        $sql = "delete from {$this->table_name} {$where_sql}";
        return $this->query($sql);
    }

    /**
     * 创建SQL
     * @param string $table 表名
     * @param string $fields 字段
     * @param mix $where 多种类型
     * @return string SQL
     */
    private function create_sql($fields, $where) {
        $sql = 'select ' . $this->fileds_sql($fields) .' from ' . $this->table_name . $this->where_sql_and($where);        
        return $sql;
    }
    /**
     * 创建字段 字符串
     * @param mix $fields 支持多种类型
     * @return string 字符串
     */
    private function fields_create($fields) {
        if($fields) {
            if(is_array($fields)) {
                $sql_arr = '';
                foreach ($fields as $key => $value) {
                    $sql_arr[] = $this->parseKey($key) . "='{$value}'";
                }
                return implode(',', $sql_arr);
            } else {
                return $fields;
            }
        }
        return '';
    }
    /**
     * 构建where and字符串
     * @param mix $where 支持array,string
     * @return string 字符串
     */
    private function where_sql_and($where) {
        if($where) {
            if(is_array($where)) {
                $where_str = ' where ';
                $sql_arr = '';
                foreach ($where as $key => $value) {
                    $sql_arr[] = $this->parseKey($key) . "='{$value}'";
                }
                $where_str .= implode(' and ', $sql_arr);
                return $where_str;
            } else {
                return $where;
            }
        }
        return '';
    }
    /**
     * 构建where or 字符串
     * @param mix $where 支持array,string
     * @return string 字符串
     */
    private function where_sql_or($where) {
        if($where) {
            $sql_arr = '';
            foreach ($where as $key => $value) {
                $sql_arr[] = $this->parseKey($key) . "='{$value}'";
            }
            return implode(' or ', $sql_arr);
        }
        return false;
    }
    /**
     * 字段和表名处理添加`
     * @access protected
     * @param string $key
     * @return string
     */
    protected function parseKey(&$key) {
        $key   =  trim($key);
        if(!preg_match('/[,\'\"\*\(\)`.\s]/',$key)) {
           $key = '`'.$key.'`';
        }
        return $key;
    }
    /**
     * 构建 fields 字符串
     * @param mix $fields 支持array, string, null, *
     * @return string 字符串
     */
    private function fileds_sql($fields) {
        if(count($fields) == 0 || empty($fields)) {
            return '*';
        }
        if(is_array($fields)) {
            return implode(',', $fields);
        }
        if(is_string($fields)) {
            return $fields;
        }
    }
}